//
//  ViewController.swift
//  Demo-12 识别设备倾斜手势
//
//  Created by quanhao huang on 2019/10/14.
//  Copyright © 2019 demo. All rights reserved.
//

import UIKit
import CoreMotion

class ViewController: UIViewController {

    @IBOutlet weak var pitchLb: UILabel!
    @IBOutlet weak var yawLb: UILabel!
    @IBOutlet weak var rollLb: UILabel!
    
    var motionManager = CMMotionManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let mainQueue = OperationQueue.main
        motionManager.startDeviceMotionUpdates(to: mainQueue) {
            (motion, error) in
            
            //翻滚
            let roll = motion!.attitude.roll
            let rollDegrees = roll * 180 / Double.pi
            //偏航
            let yaw = motion!.attitude.yaw
            let yawDegrees = yaw * 180 / Double.pi
            //俯仰
            let pitch = motion!.attitude.pitch
            let pitchDegrees = pitch * 180 / Double.pi
            
            self.rollLb.text = String(format: "Roll:%.2f", rollDegrees)
            self.yawLb.text = String(format: "Yaw:%.2f", yawDegrees)
            self.pitchLb.text = String(format: "Pitch:%.2f", pitchDegrees)
        }
        
    }


}

